package com.example.personalcenter.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.personalcenter.R;
import com.example.personalcenter.Tool.OKHttpTool;
import com.example.personalcenter.onlyPerson.LoginActivity;
import com.example.personalcenter.onlyPerson.bean.UserDataBean;


public class MeFragment extends Fragment {
    private ImageView image;
    private TextView username;
    private LinearLayout meInfo;
    private LinearLayout indentInfo;
    private LinearLayout paswInfo;
    private LinearLayout opinionInfo;
    private Button btn_exit;
    private View view;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private Handler handler;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_me, container, false);
        initView();
        isLogin();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private void isLogin() {
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("登录".equals(btn_exit.getText())) {
                    // 跳转到登录界面
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    getActivity().startActivity(intent);
                } else if ("退出登录".equals(btn_exit.getText().toString())) {
                    // 退出登录
                    logout();
                }
            }
        });
    }

    private void updateUI() {
        if (TextUtils.isEmpty(OKHttpTool.TOKEN)) {
            // 未登录状态
            clearUserData();
            btn_exit.setText("登录");
            username.setText("请先登录");
        } else {
            // 已登录状态
            getUserData();
            btn_exit.setText("退出登录");
        }
    }



    private void getUserData() {
        String url = "prod-api/api/common/user/getInfo";
        OKHttpTool.deNetRequest(url, OKHttpTool.GET, UserDataBean.class, new OKHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        UserDataBean userDataBean = (UserDataBean) obj;
                        if (userDataBean.getCode() == 200) {
                            username.setText(userDataBean.getUser().getNickName());
                        } else {
                            clearUserData();
                            username.setText("请先登录");
                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {
                // 处理请求失败的情况
            }
        });
    }



    private void clearUserData() {
        // 清除本地存储的用户信息，例如 SharedPreferences 中的数据
        SharedPreferences preferences = requireContext().getSharedPreferences("user_data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();

        OKHttpTool.TOKEN = ""; // 保持一致，避免空指针异常
    }

    private void logout() {
        // 用户点击退出登录时的逻辑
        clearUserData();
        OKHttpTool.TOKEN = "";
        updateUI(); // 更新 UI 显示为未登录状态
    }


    private void initView() {


        image = view.findViewById(R.id.image);
        username = view.findViewById(R.id.name);
        meInfo = view.findViewById(R.id.me_info);
        indentInfo = view.findViewById(R.id.indent_info);
        paswInfo = view.findViewById(R.id.pasw_info);
        opinionInfo = view.findViewById(R.id.opinion_info);
        btn_exit = view.findViewById(R.id.btn_exit);

        handler = new Handler();

        sharedPreferences = getActivity().getSharedPreferences("login", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }


}